/*

File name		: tableC1.do

Description		: conducts balance of covariates checks of weather instrument

Output File		: tableC1.tex
				
*/

set more off, perm
capture restore

*** 001 Analysis/Tabulation ***
use "data/blood_donation/blood_donation.dta", clear

	// Merge rainfall data
	merge m:1 entnahmedatum using "data/rainfall/rainfallZH.dta"
	*, keep(match)

	*rename kalendertag daily_rainfall
	rename daily_rainfall_ZH daily_rainfall
	gen any_rain = 1 if daily_rainfall > 0 & daily_rainfall != .
	replace any_rain = 0 if daily_rainfall == 0
	sort spnr entnahmedatum
	gen any_rain_plus1 = any_rain[_n+1] if spnr == spnr[_n+1]

	sort spnr entnahmedatum
	gen rainfall_plus1 = daily_rainfall[_n+1] if spnr == spnr[_n+1]
	gen strong_rain = 1 if daily_rainfall >= 10
	replace strong_rain = 0 if daily_rainfall < 10

	// Robustnesss: Extreme heat and cold. 
	gen extreme_cold = 1 if daily_high_ZH < 5 & daily_high_ZH != .
	replace extreme_cold = 0 if daily_high_ZH >= 5 & daily_high_ZH != .

	replace daily_low_ZH = "." if daily_low_ZH == "-"
	destring daily_low_ZH, replace
	gen frost_day = 1 if daily_low_ZH < 0 & daily_low_ZH != .
	replace frost_day = 0 if daily_low_ZH >= 0 & daily_low_ZH != .

	gen extreme_heat = 1 if daily_high_ZH > 25 & daily_high_ZH != .
	replace extreme_heat = 0 if daily_high_ZH <= 25 & daily_high_ZH != .

	gen adverse_weather = 0 if extreme_cold != .
	replace adverse_weather = 1 if extreme_cold == 1 | extreme_heat == 1 | strong_rain == 1
	
	// Generate Controls
	gen spring=1 if period==1|period==3
	replace spring=0 if spring==.
	gen month=month(entnahmedatum)
	gen week=week(entnahmedatum)
	tab month, gen(month_dummy)
	tab c_num, gen(c_num_dummy)

	label define order 1 "K" 2 "1a" 3 "1b" 4 "2"
	encode treatment, gen(treatment_ordered) label(order)
	label values treatment_ordered order	
	
	lab var adverse_weather "Adverse Weather"
	lab var phonecall "Call"
	lab var age "Age"
	lab var male "Male"
	lab var bltype_dummy1 "O+ Blood Type"
	lab var bltype_dummy2 "O- Blood Type"
	lab var bltype_dummy3 "A- Blood Type"

balancetable adverse_weather phonecall age male bltype_dummy1 bltype_dummy2 bltype_dummy3 using "output/tables/tableC1.tex" if period<3, format(%5.4f) replace ctitles("No adverse weather" "Adverse weather" "Difference") varlabels
